﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using E2_WEBSITE.Code.Admin;
using E2_WEBSITE.Code;

public partial class cp_admin_site_newsmanagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
        {
            WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
            return;
        }
        Page.Response.Redirect("~/cp/admin/site/addnews.aspx");
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
        {
            WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
            return;
        }
        try
        {
            if (gridNews.Rows.Count > 0)
            {
                NewsController controller = new NewsController();
                for (int i = 0; i < gridNews.Rows.Count; i++)
                {
                    CheckBox cb = gridNews.Rows[i].FindControl("AdsSelector") as CheckBox;
                    if (cb != null && cb.Checked)
                    {
                        int nid = Convert.ToInt32(gridNews.DataKeys[i].Value);
                        controller.RemoveNews(nid);
                    }
                }
                Page.Response.Redirect("~/cp/admin/site/newsmanagement.aspx");
            }
        }
        catch (Exception)
        {
            Response.Redirect(Utilities.ErrorPagePath);
            throw;
        }
    }

    protected void btnSelectAll_Click(object sender, EventArgs e)
    {
        ToggleNewsSelector(true);
    }

    protected void btnDeselectAll_Click(object sender, EventArgs e)
    {
        ToggleNewsSelector(false);
    }

    private void ToggleNewsSelector(bool checkState)
    {
        if (gridNews.Rows.Count > 0)
        {
            for (int i = 0; i < gridNews.Rows.Count; i++)
            {
                CheckBox cb = gridNews.Rows[i].FindControl("AdsSelector") as CheckBox;
                if (cb != null)
                {
                    cb.Checked = checkState;
                }
            }
        }
    }
}
